package org.jeecg.modules.hc.service.impl;

import com.github.binarywang.wxpay.bean.request.WxPayRefundV3Request;
import com.github.binarywang.wxpay.bean.result.WxPayOrderQueryV3Result;
import com.github.binarywang.wxpay.bean.result.WxPayRefundQueryV3Result;
import com.github.binarywang.wxpay.bean.result.WxPayRefundV3Result;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import me.chanjar.weixin.common.util.RandomUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.hc.entity.HcWxPay;
import org.jeecg.modules.hc.entity.HcWxPayRefund;
import org.jeecg.modules.hc.mapper.HcWxPayRefundMapper;
import org.jeecg.modules.hc.service.IHcWxPayRefundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Description: 微信退款
 * @Author: jeecg-boot
 * @Date: 2023-01-09
 * @Version: V1.0
 */
@Service
public class HcWxPayRefundServiceImpl extends ServiceImpl<HcWxPayRefundMapper, HcWxPayRefund> implements IHcWxPayRefundService {

    @Autowired
    private WxPayService payService;

    public HcWxPayRefund refund(String outTradeNo, String userId, Integer total, BigDecimal refund) throws WxPayException {
        String outRefundNo = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + RandomUtils.getRandomStr();
        String notifyUrl = "https://www.weixin.qq.com/wxpay/pay.php";
        String currency = "CNY";
        String reason = "取消订单";

        WxPayRefundV3Request request = new WxPayRefundV3Request();
        request.setOutTradeNo(outTradeNo);
        request.setOutRefundNo(outRefundNo);
        request.setReason(reason);
        request.setNotifyUrl(notifyUrl);

        //构建金额信息
        WxPayRefundV3Request.Amount amount = new WxPayRefundV3Request.Amount();
        //设置币种信息
        amount.setCurrency(currency);
        //设置金额
        amount.setTotal(total);
        int refundInt = refund.multiply(new BigDecimal("100")).intValue();
        amount.setRefund(refundInt);

        request.setAmount(amount);

        HcWxPayRefund wxPayRefund = new HcWxPayRefund().setUserId(userId).setOutTradeNo(outTradeNo)
                .setOutRefundNo(outRefundNo).setReason(reason).setRefund(refundInt)
                .setNotifyUrl(notifyUrl).setAmountTotal(total).setAmountCurrency(currency);

        WxPayRefundV3Result wxPayRefundV3Result = this.payService.refundV3(request);
        wxPayRefund.setRefundId(wxPayRefundV3Result.getRefundId()).setChannel(wxPayRefundV3Result.getChannel());
        this.save(wxPayRefund);
        return wxPayRefund;
    }

    public HcWxPayRefund queryRefund(String refundId) {
        HcWxPayRefund hcWxPayRefund = this.getById(refundId);

        if (hcWxPayRefund == null) {
            throw new JeecgBootException("订单号不存在");
        }

        WxPayRefundQueryV3Result wxPayRefundQueryV3Result = null;
        try {
            wxPayRefundQueryV3Result = this.payService.refundQueryV3(hcWxPayRefund.getOutRefundNo());
        } catch (WxPayException e) {
            throw new RuntimeException(e);
        }

        HcWxPayRefund update = new HcWxPayRefund().setId(hcWxPayRefund.getId())
                .setStatus(wxPayRefundQueryV3Result.getStatus());

        this.updateById(update);
        return update;
    }
}
